package com.wtwd.campus.fjryE15.controller;

import com.wtwd.campus.fjryE15.entity.FjryRespCode;
import com.wtwd.campus.fjryE15.entity.FjryResultVO;
import com.wtwd.campus.fjryE15.entity.dto.UnbindStudentCardDto;
import com.wtwd.campus.fjryE15.service.DeviceService;
import com.wtwd.campus.fjryE15.service.FjryAuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author mjy
 * @date 2022/09/26
 * @description
 */
@RestController
@RequestMapping("/fjry")
public class FjryDeviceController {

    private static final Logger logger = LoggerFactory.getLogger(FjryDeviceController .class);

    @Resource
    private FjryAuthService fjryAuthService;


    @Resource
    private DeviceService deviceService;

    /**
     * 福建睿翼解绑学生证
     *
     * @param unbindStudentCardDto
     * @return
     */
    @PostMapping("/unbindStudentCard")
    public FjryResultVO unbindStudentCard(@RequestBody UnbindStudentCardDto unbindStudentCardDto) {
        logger.info("fjry unbindStudentCard={}", unbindStudentCardDto.toString());
        if (!fjryAuthService.authRequest(unbindStudentCardDto.getTime(), unbindStudentCardDto.getRandomStr(), unbindStudentCardDto.getSign())) {
            return FjryResultVO.error(FjryRespCode.NO_RIGHT_OPERATION);
        }
        String imei = unbindStudentCardDto.getImei();
        if (!deviceService.authDeviceIsExist(imei)) {
            return FjryResultVO.error(FjryRespCode.DEVICE_NOT_EXIST);
        }
        boolean online = deviceService.authDeviceIsOnline(imei);
        if (!online) {
            return FjryResultVO.error(FjryRespCode.DEVICE_NOT_ONLINE);
        }
        try {
            deviceService.unbindStudentCard(imei);
        } catch (Exception e) {
            e.printStackTrace();
            return FjryResultVO.error(FjryRespCode.FAILED);
        }
        return FjryResultVO.ok();
    }
}
